package controllers

import (
	"gitee.com/igolang/imoney/internal/app/system/services"
	"gitee.com/igolang/imoney/internal/common/httpResp"
	"github.com/gin-gonic/gin"
)

var Role = &role{}

type role struct{}

func (c *role) List(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	data, err := services.Role.List(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
	r.Data = data
}

func (c *role) DeptTree(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	data, err := services.Role.DeptTree(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
	r.Data = data
}

func (c *role) ChangeStatus(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	if err := services.Role.ChangeStatus(ctx); err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}

func (c *role) DataScope(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	err := services.Role.DataScope(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}

func (c *role) Export(ctx *gin.Context) {
	r := httpResp.Response()
	b, err := services.Role.Export(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		httpResp.JSON(ctx, r)
		return
	}
	httpResp.Export(ctx, b, "xlsx")
}

func (c *role) Detail(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	data, err := services.Role.Detail(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
	r.Data = data
}

func (c *role) Save(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	if err := services.Role.Save(ctx); err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}

func (c *role) Delete(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	if err := services.Role.Delete(ctx); err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}

func (c *role) AllocatedList(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	data, err := services.Role.AllocatedList(ctx, false)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
	r.Data = data
}

func (c *role) UnallocatedList(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	data, err := services.Role.AllocatedList(ctx, true)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
	r.Data = data
}

func (c *role) AuthSelectAll(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	err := services.Role.SelectAll(ctx, false)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}

func (c *role) AuthCancelAll(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	err := services.Role.SelectAll(ctx, true)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}

func (c *role) AuthCancel(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	err := services.Role.CancelAuth(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}
